package org.jypj.dev.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jypj.dev.model.TrainWork;
import org.jypj.dev.model.UserWork;
import org.jypj.dev.vo.FileTypeVo;
import org.jypj.dev.vo.UserWorkTree;

/**
 * 用户工序的类 计算所有关于userWork的类
 *
 * @author ChenYu
 */
public class UserWorkUtils {

    /**
     * 更新用户工序完成数量 select '0' as id,'日巡检监测工序' as name,''as parentId, '' as
     * workId,''as workName,'' as workType, 0 as isFinish,0 as isCatelog from
     * (select @r=0) tt UNION all
     *
     * @return
     */
    public static List<UserWorkTree> initUserWorkTreeFinishNum(List<UserWorkTree> userWorkTrees) {
        for (UserWorkTree userWorkTree : userWorkTrees) {
            String name = userWorkTree.getName();
            int finishNum = 0;
            if (userWorkTree.getIsCatalog() == 0) {
                for (UserWorkTree uwt : userWorkTrees) {
                    if (uwt.getId().startsWith(userWorkTree.getId()) && uwt.getIsCatalog() == 1 && uwt.getWorkStatus() >= 1) {
                        finishNum++;
                    }
                }
            } else if (userWorkTree.getIsCatalog() == 1 && userWorkTree.getWorkStatus() >= 1) {
                finishNum = 1;
            }
            name = name + " " + finishNum;
            userWorkTree.setName(name);
        }
        // 添加住菜单
        UserWorkTree userWorkTree = new UserWorkTree("0", "日巡检监测工序", "");
        userWorkTrees.add(userWorkTree);

        return userWorkTrees;
    }

    /**
     * @param fileTypeVos
     * @return
     */
    public static List<UserWorkTree> getNumFromFileVos(List<FileTypeVo> fileTypeVos) {
        List<UserWorkTree> userWorkTrees = new ArrayList<>();
        if (fileTypeVos != null && fileTypeVos.size() > 0) {
            UserWorkTree userWorkTree2 = new UserWorkTree("tempWork", "临时工序", "");
            userWorkTrees.add(userWorkTree2);
            for (FileTypeVo fileTypeVo : fileTypeVos) {
                String name = "";
                switch (fileTypeVo.getFileType()) {
                    case "jpg":
                        name = "拍照";
                        break;
                    case "mp3":
                        name = "录音";
                        break;
                    case "mp4":
                        name = "视频";
                        break;
                }
                UserWorkTree u = new UserWorkTree(fileTypeVo.getFileType(), name + " " + fileTypeVo.getNum(), "tempWork");
                userWorkTrees.add(u);
            }
        }
        return userWorkTrees;
    }

    /**
     * @param fileTypeVos
     * @return
     */
    public static List<UserWorkTree> getNumFromEmergeFileVos(List<FileTypeVo> fileTypeVos) {
        List<UserWorkTree> userWorkTrees = new ArrayList<>();
        if (fileTypeVos != null && fileTypeVos.size() > 0) {
            UserWorkTree userWorkTree2 = new UserWorkTree("emerge", "应急文件", "");
            userWorkTrees.add(userWorkTree2);
            for (FileTypeVo fileTypeVo : fileTypeVos) {
                String name = "";
                switch (fileTypeVo.getFileType()) {
                    case "jpg":
                        name = "拍照";
                        break;
                    case "mp3":
                        name = "录音";
                        break;
                    case "mp4":
                        name = "视频";
                        break;
                }
                UserWorkTree u = new UserWorkTree(fileTypeVo.getFileType(), name + " " + fileTypeVo.getNum(), "emerge");
                userWorkTrees.add(u);
            }
        }
        return userWorkTrees;
    }

    /**
     * @return
     */
    public static List<UserWorkTree> getNumFrom181() {
        List<UserWorkTree> userWorkTrees = new ArrayList<>();
        UserWorkTree userWorkTree = new UserWorkTree("2", "181故障 0", "");
        userWorkTrees.add(userWorkTree);
        return userWorkTrees;
    }

    /**
     * 从userWork列表中得到工序的map集合
     *
     * @param userWorks
     * @return
     */
    public static Map<String, List<UserWork>> getMapFromUserWorks(List<UserWork> userWorks) {
        Map<String, List<UserWork>> map = new HashMap<>();
        for (UserWork userWork : userWorks) {
            String key = userWork.getLoginId() + "-" + userWork.getTrainCode() + "-" + userWork.getTrainWorkId();
            List<UserWork> list = map.get(key);
            if (list == null) {
                list = new ArrayList<>();
                list.add(userWork);
                map.put(key, list);
            } else {
                list.add(userWork);
            }
        }
        return map;
    }

    /**
     * 设置一级工序的完成状态
     *
     * @param trainWorks
     * @return
     */
    public static List<UserWork> initUserWorkStatus(List<UserWork> trainWorks) {
        for (UserWork trainWork : trainWorks) {
            if (trainWork.getWorkStatus() >= 1) {
                // 判断是延迟还是提前
                int planStartTime = DateUtils.getSecondFromTime(trainWork.getStartTime() + ":00");
                int planEndTime = DateUtils.getSecondFromTime(trainWork.getEndTime() + ":00");
                int finishTime = DateUtils.getSecondFromTime(trainWork.getFinishTime());
                int beginTime = DateUtils.getSecondFromTime(trainWork.getBeginTime());
                if (planStartTime > beginTime) {
                    trainWork.setWorkStatus(3);
                    continue;
                }
                if (finishTime > planEndTime) {
                    trainWork.setWorkStatus(4);
                    continue;
                }
                if (beginTime >= planStartTime && finishTime <= planEndTime) {
                    trainWork.setWorkStatus(2);
                    continue;
                }

            } else if (StringUtil.isEmpty(trainWork.getBeginTime())) {
                trainWork.setWorkStatus(0);
            } else {
                trainWork.setWorkStatus(1);
            }
        }
        return trainWorks;
    }

    /**
     * 初始化目录的开始和结束时间以及是否完成
     *
     * @param userWorks
     */
    public static void initUserWorkCatelogBeginFinish(List<UserWork> userWorks) {
        for (UserWork userWork : userWorks) {
            if (userWork.getIsCatelog() == 0) {
                List<UserWork> uws = new ArrayList<>();
                boolean isFinish = true;
                for (UserWork uw : userWorks) {
                    if (uw.getTrainWorkId().equals(userWork.getTrainWorkId())
                            && uw.getWorkId().startsWith(userWork.getWorkId()) && uw.getIsCatelog() == 1) {
                        uws.add(uw);
                        if (uw.getWorkStatus() == 0) {
                            isFinish = false;
                            break;
                        }
                    }
                }
                int size = uws.size();
                if (size > 0) {
                    String beginTime = "";
                    for (int i = 0; i < size; i++) {
                        if (StringUtil.isEmpty(uws.get(i).getBeginTime())) {
                            continue;
                        } else {
                            beginTime = uws.get(i).getBeginTime();
                            break;
                        }
                    }
                    userWork.setBeginTime(beginTime);
                    if (isFinish) {
                        userWork.setWorkStatus(2);
                        userWork.setFinishTime(uws.get(size - 1).getFinishTime());
                    }
                }
            }
        }
    }

    /**
     * 初始化用户工序的时间间隔
     *
     * @param userWorks
     * @throws ParseException
     */
    public static void initUserWorkTimeSpan(List<UserWork> userWorks) {
        int size = userWorks.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                UserWork userWork = userWorks.get(i);
                if (userWork.getIsCatelog() == 0) {// 判断如果是目录
                    String beginTime = userWork.getBeginTime();
                    String finishTime = userWork.getFinishTime();
                    if (StringUtil.isNotEmpty(beginTime) && StringUtil.isNotEmpty(finishTime)) {
                        int timeSpan = DateUtils.getDateOffset(finishTime, beginTime);
                        userWork.setTimeSpan(timeSpan);
                    } else {
                        userWork.setTimeSpan(0);
                    }
                } else if (userWork.getIsCatelog() == 1) {// 如果是功能
                    UserWork userWork2 = userWorks.get(i - 1);
                    String finishTime1 = userWork.getFinishTime();
                    if (userWork2.getIsCatelog() == 0 || StringUtil.isEmpty(finishTime1)) {
                        userWork.setTimeSpan(0);
                    } else {
                        String finishTime2 = userWork2.getFinishTime();// 上一个工序的完成时间
                        if (StringUtil.isEmpty(finishTime2)) {
                            for (int j = 2; j <= 99; j++) {
                                UserWork userWork3 = userWorks.get(i - j);
                                if (userWork3.getIsCatelog() == 0) {
                                    userWork.setTimeSpan(0);
                                    break;
                                } else if (userWork3.getIsCatelog() == 1) {
                                    String finishTime3 = userWork3.getFinishTime();
                                    if (StringUtil.isEmpty(finishTime3)) {
                                        continue;
                                    } else {
                                        int timeSpan = DateUtils.getDateOffset(finishTime1, finishTime3);
                                        userWork.setTimeSpan(timeSpan);
                                        break;
                                    }
                                }
                            }
                        } else {
                            int timeSpan = DateUtils.getDateOffset(finishTime1, finishTime2);
                            userWork.setTimeSpan(timeSpan);
                        }

                    }
                }
            }
        }

    }


    /**
     * 设置userWork的分数
     */
    public static void initUserWorkScore(List<UserWork> userWorks) {
        for (UserWork userWork : userWorks) {
            int score = 0;
            if (userWork.getCatalogDepth() == 1) {
                if (userWork.getWorkStatus() == 0 || userWork.getWorkStatus() == 1) {
                    score += userWork.getNumLimitOne();
                }
                if (userWork.getWorkStatus() == 3 || userWork.getWorkStatus() == 4) {
                    score += userWork.getNumScoreOne();
                }

            } else {
                if (userWork.getTimeSpan() < userWork.getTimeLimitOne()) {
                    score += userWork.getTimeScoreOne();
                } else if (userWork.getTimeSpan() >= userWork.getTimeLimitOne()
                        && userWork.getTimeSpan() < userWork.getTimeLimitTwo()) {
                    score += userWork.getTimeScoreTwo();
                } else if (userWork.getTimeSpan() >= userWork.getTimeLimitTwo()) {
                    score += userWork.getTimeScoreThree();
                }
            }
            userWork.setScore(score);

        }

    }

    /**
     * 设置userWork中一级工序的分数
     *
     * @param userWorks
     */
    public static void initUserWorkDepth1Score(List<UserWork> userWorks) {
        for (UserWork userWork : userWorks) {
            if (userWork.getCatalogDepth() == 1) {
                int score = userWork.getScore();
                for (UserWork uw : userWorks) {
                    if (!userWork.getId().equals(uw.getId()) && uw.getWorkId().startsWith(userWork.getWorkId())
                            && uw.getTrainWorkId().equals(userWork.getTrainWorkId())) {
                        score += uw.getScore();
                    }
                }
                userWork.setScore(score);
            }
        }
    }

}
